#ifndef SHOWPIC_H
#define SHOWPIC_H

#include <QWidget>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QImage>
#include <QTimer>
#include <QPoint>

#include <vector>
#include <iostream>
using namespace std;


typedef struct
{
    double x;
    double y;
    double z;
    int r,g,b;
}POINT3D1;

typedef struct
{
    double x;
    double y;
}POINT1;


namespace Ui {
class ShowPic;
}

class ShowPic : public QWidget
{
    Q_OBJECT

public:
    explicit ShowPic(QWidget *parent = 0);
    ~ShowPic();

    void keyPressEvent(QKeyEvent *ev);
    void paintEvent(QPaintEvent *);

    void RotateX(POINT3D1 &p, double angle);
    void RotateY(POINT3D1 &p, double angle);
    void RotateZ(POINT3D1 &p, double angle);
    POINT1 Projection(POINT3D1 p);

    void init(char imgName[], char backName[])
    {
        point_num=0;

        _angle_x = 0;
        _angle_y = 0;
        _angle_z = 0;
        viewZ=3;
        rate=1;
        pen_width=1;

        QImage img(imgName);
        w=img.width();
        h=img.height();

        p3d=(POINT3D1 *)malloc(sizeof(POINT3D1)*(w+1)*(h+1));
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                p3d[point_num].r=qRed(img.pixel(i,j));
                p3d[point_num].g=qGreen(img.pixel(i,j));
                p3d[point_num].b=qBlue(img.pixel(i,j));

                point_num++;
            }
        }

        back.load(backName);

        rate_h=1;
        getPoints();
    }

    void getPoints()
    {
        point_num=0;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                p3d[point_num].x=(1.0*i-w*0.5)/(w*0.5);
                p3d[point_num].y=(1.0*j-h*0.5)/(h*0.5);
                p3d[point_num].z=(1.0*qRed(back.pixel(i,j))-128)/128.0*rate_h;
                point_num++;

            }
        }
    }

    POINT3D1 *p3d;
    int point_num;

    double _angle_x;
    double _angle_y;
    double _angle_z;

    double viewZ;

    double rate;
    int pen_width;

    double rate_h;

    int w,h;
    QImage back;

private:
    Ui::ShowPic *ui;
};

#endif // SHOWPIC_H
